package com.yunjian.core.basic.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunjian.core.basic.mapper.SResourceMapper;
import com.yunjian.core.basic.model.SResource;
import com.yunjian.core.basic.model.SResourceExample;
import com.yunjian.core.basic.model.SRole;
import com.yunjian.core.basic.model.SRoleExample;
import com.yunjian.core.basic.model.SRoleResourceExample;
import com.yunjian.core.basic.model.SRoleResourceKey;
import com.yunjian.core.basic.model.SUserRoleExample;
import com.yunjian.core.basic.model.SUserRoleKey;
import com.yunjian.core.basic.service.CacheService;
import com.yunjian.core.basic.service.SResourceService;
import com.yunjian.core.basic.service.SRoleResourceService;
import com.yunjian.core.basic.service.SRoleService;
import com.yunjian.core.basic.service.SUserRoleService;
import com.yunjian.core.basic.vo.CacheType;

@Service
public class SResourceServiceImpl implements SResourceService{
	
	//private final static Logger logger = LoggerFactory.getLogger(SResourceServiceImpl.class);
	
	@Autowired
	private SResourceMapper mapper;
	@Autowired
	private SUserRoleService sUserRoleService;
	@Autowired
	private SRoleResourceService sRoleResourceService;
	@Autowired
	private SRoleService sRoleService;
	@Autowired 
	private CacheService cacheService;
	
	@Override
	public int countByExample(SResourceExample example) {
		return mapper.countByExample(example);
	}

	@Override
	public int deleteByExample(SResourceExample example) {
		return mapper.deleteByExample(example);
	}

	@Override
	public int deleteByPrimaryKey(String userId) {
		return mapper.deleteByPrimaryKey(userId);
	}

	@Override
	public int insert(SResource record) {
		return mapper.insert(record);
	}

	@Override
	public int insertSelective(SResource record) {
		return mapper.insertSelective(record);
	}

	@Override
	public List<SResource> selectByExample(SResourceExample example) {
		return mapper.selectByExample(example);
	}

	@Override
	public SResource selectByPrimaryKey(String userId) {
		return mapper.selectByPrimaryKey(userId);
	}

	@Override
	public int updateByExampleSelective(SResource record, SResourceExample example) {
		return mapper.updateByExampleSelective(record, example);
	}

	@Override
	public int updateByExample(SResource record, SResourceExample example) {
		return mapper.updateByExample(record, example);
	}

	@Override
	public int updateByPrimaryKeySelective(SResource record) {
		return mapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(SResource record) {
		return mapper.updateByPrimaryKey(record);
	}

	@Override
	public List<SResource> selectByUserId(String userId) {
		// 查询用户所有角色
		SUserRoleExample urEx = new SUserRoleExample();
		urEx.createCriteria().andUserIdEqualTo(userId);
		List<SUserRoleKey> urList = sUserRoleService.selectByExample(urEx);
		if (urList == null || urList.isEmpty()) {
			return null;
		}
		List<String> roleIdList = new ArrayList<String>();
		for (SUserRoleKey key : urList) {
			roleIdList.add(key.getRoleId());
		}
		
		// 查询所有角色是否有效
		SRoleExample rEx = new SRoleExample();
		rEx.createCriteria().andStatusEqualTo(1).andRoleIdIn(roleIdList);
		List<SRole> rList = sRoleService.selectByExample(rEx);
		if (rList == null || rList.isEmpty()) {
			return null;
		}
		roleIdList.clear();
		for (SRole role : rList) {
			roleIdList.add(role.getRoleId());
		}
		
		// 查询拥有角色的所有资源
		SRoleResourceExample rrEx = new SRoleResourceExample();
		rrEx.createCriteria().andRoleIdIn(roleIdList);
		List<SRoleResourceKey> rrList = sRoleResourceService.selectByExample(rrEx);
		if (rrList == null || rrList.isEmpty()) {
			return null;
		}
		List<String> resIdList = new ArrayList<String>();
		for (SRoleResourceKey key : rrList) {
			resIdList.add(key.getResId());
		}
		
		// 查询拥有的所有资源
		SResourceExample reEx = new SResourceExample();
		reEx.createCriteria().andResIdIn(resIdList);
		reEx.setDistinct(true);
		
		return mapper.selectByExample(reEx);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SResource> selectCacheByUserId(String userId) {
		Object cache = cacheService.getCache(CacheType.RMS_MENU.getCode(), userId);
		if (cache != null) {
			return (List<SResource>)cache;
		}
		cache = this.selectByUserId(userId);
		cacheService.addCache(CacheType.RMS_MENU.getCode(), userId, cache);
		return (List<SResource>)cache;
	}

	@Override
	public PageInfo<SResource> selectPagedByExample(int page, int rows,
			SResourceExample example) {
		PageHelper.startPage(page, rows);
		PageInfo<SResource> pageInfo = new PageInfo<>(mapper.selectByExample(example));
		return pageInfo;
	}
	
}
